package org.easy.db.generator;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.easy.db.generator.util.FieldUtil;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Description: 数据库表信息
 * @Author: wxl
 * @CreateTime: 2023/1/19 10:52
 */
@Data
public class TableDefinition {

    /**
     * 表名
     */
    private String tableName;

    /**
     * 模块
     */
    private String moduleName;

    /**
     * 实体名称
     */
    private String entityName;

    /**
     * mapper名称
     */
    private String mapperName;

    /**
     * xml名称
     */
    private String xmlName;

    /**
     * service名称
     */
    private String serviceName;

    /**
     * serviceImpl名称
     */
    private String serviceImplName;

    /**
     * controller名称
     */
    private String controllerName;

    /**
     * 包名
     */
    private String parentPackage;

    /**
     * 需要生成的字段
     */
    private List<ColumnDefinition> columnDefinitionList;
    /**
     * 需要导入的JAVA类
     */
    private Set<String> importJavaClassList = new TreeSet<>();

    public TableDefinition (String tableName,String moduleName, String parentPackage, Map<String,String> templateMap) {
        this.tableName = tableName;
        this.parentPackage = parentPackage;
        if (StringUtils.isNotEmpty(moduleName)) {
            this.moduleName = moduleName;
        }
        this.initName(templateMap);
    }

    private void initName(Map<String,String> templateMap) {
        if (null == templateMap) {
            templateMap = new ConcurrentHashMap<>(1 << 8);
        }
        String packagePath = this.parentPackage.replace(".",File.separator);
        String name = FieldUtil.upperFirstLetter(FieldUtil.underlineFilter(this.tableName));
        if (StringUtils.isEmpty(this.moduleName)) {
            this.moduleName = name.toLowerCase();
        }
        this.entityName = name + "Entity";
        templateMap.put("/template/entity.java.vm",packagePath+File.separator+this.moduleName+File.separator+"entity"+File.separator+this.entityName+".java");
        this.mapperName = name + "Mapper";
        templateMap.put("/template/mapper.java.vm",packagePath+File.separator+this.moduleName+File.separator+"mapper"+File.separator+this.mapperName+".java");
        this.serviceName = name + "Service";
        templateMap.put("/template/service.java.vm",packagePath+File.separator+this.moduleName+File.separator+"service"+File.separator+this.serviceName+".java");
        this.controllerName = name + "Controller";
        templateMap.put("/template/controller.java.vm",packagePath+File.separator+this.moduleName+File.separator+"controller"+File.separator+this.controllerName+".java");
        this.serviceImplName = name + "ServiceImpl";
        templateMap.put("/template/serviceImpl.java.vm",packagePath+File.separator+this.moduleName+File.separator+"service"+File.separator+"impl"+File.separator+this.serviceImplName+".java");
    }

    public void setColumnDefinitionList(List<ColumnDefinition> columnDefinitionList) {
        this.columnDefinitionList = columnDefinitionList;
        Set<String> set = this.columnDefinitionList
                .stream()
                .filter(ColumnDefinition::isNotNothing)
                .map(ColumnDefinition::getJavaPackage)
                .collect(Collectors.toSet());
        this.importJavaClassList.addAll(set);
    }
}
